#include <QtGui>

#include "qbighugewidget.h"

// parent window is passed to QDialog parent class
QBigHugeWidget::QBigHugeWidget(QWidget *parent) : QListView(parent) {
        // wire up the Big Huge interface
        connect(    &bigHuge,
                    SIGNAL(synonymsReceived(QStringList)),
                    this,
                    SLOT(success(QStringList)));
        connect(    &bigHuge,
                    SIGNAL(error(QBigHuge::Error,QString)),
                    this,
                    SLOT(failure(QBigHuge::Error,QString)));
        setWindowTitle("BigHuge Thesaurus");
}

// download success
void QBigHugeWidget::success(QStringList synonyms) {
    foreach (QString synonym, synonyms) {
        // create a new item. will be nuked by gc.
        // set 'display' role data to 'synonym'
        QStandardItem *item = new QStandardItem(synonym);
        // configure item to be checkable
        item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
        // set the check state role to 'checked'.
        // 'items' can contain different items of data under different
        // roles.
        // the 'display' role contains the QVariant string to show.
        // the 'checkstate' role contains the QVariant check status
        item->setData(QVariant(Qt::Checked), Qt::CheckStateRole);
        // add item to data model
        if (model.findItems(synonym).size() == 0)
            model.appendRow(item);
    }
    // send data model to viewer. needed to derive from a ListView
    // to get access to setModel()
    setModel(&model);
}

void QBigHugeWidget::addSynonym(QString synonym)
{
    QStandardItem *item = new QStandardItem(synonym);
    item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
    item->setData(QVariant(Qt::Checked), Qt::CheckStateRole);
    if (model.findItems(synonym).size() == 0)
        model.appendRow(item);
    setModel(&model);
}

// return check synonyms
QStringList QBigHugeWidget::getCheckedSynonyms() {
    QStringList synonymsList;
    for (int i = 0; i < model.rowCount(); i++) {;
        // read data out of data model and into string list
        // 'row' refers to list index. 'col' is 0?
        QModelIndex index = model.index(i, 0);
        QString str = model.data(index).toString();
        QVariant checked = model.data(index, Qt::CheckStateRole);
        // only add checked items
        if (checked == Qt::Checked) {
            synonymsList << str;
        }
    }
    return synonymsList;
}

// error occurred
void QBigHugeWidget::failure(const QBigHuge::Error errorCode, const QString& errorString) {
    // examine error code
    switch (errorCode) {
    // network error
    case QBigHuge::NetworkError:
        QMessageBox::warning(   this,
                                "Big Huge: Network error",
                                errorString,
                                QMessageBox::Abort );
        break;
    // xml parsing error
    case QBigHuge::XmlError:
        QMessageBox::warning(   this,
                                "Big Huge: XML parsing error",
                                errorString,
                                QMessageBox::Abort );
        break;
    default:
        QMessageBox::warning(   this,
                                "Big Huge error",
                                "An unknown error occurred!",
                                QMessageBox::Abort );
        break;
    }
}
